from apps.common.base.serializers import *
from apps.education.course.models import *


class CourseBaseSerializer(BaseSerializer):
    class Meta:
        model = Course
        fields = "__all__"


class CourseModelSerializer(CourseBaseSerializer):

    coursecateModel = serializers.SerializerMethodField()
    def get_coursecateModel(self, obj):
        try:
            return {'id': obj.coursecate.id, 'name': obj.coursecate.name, 'key': obj.coursecate.key}
        except:
            return {}
        
    teacherModel = serializers.SerializerMethodField()
    def get_teacherModel(self, obj):
        try:
            return {'id': obj.teacher.id, 'realname': obj.teacher.realname}
        except:
            return {}
    
    tagList = serializers.SerializerMethodField()
    def get_tagList(self, obj):
        try:
            return [{'id': obj.id, 'name': obj.name, 'key': obj.key} for obj in obj.tags.all()]
        except:
            return []
        
    class Meta:
        model = Course
        fields = "__all__"
